package com.example.cart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.cart.dao.mapper.CartMapper;
import com.example.cart.dao.po.Cart;
import com.example.common.Interface.cart.entity.dto.*;
import com.example.common.Interface.cart.entity.vo.CartItemVO;
import com.example.common.Interface.cart.entity.vo.CartVO;
import com.example.common.Interface.cart.service.CartService;
import com.example.common.response.BizException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@DubboService
@Slf4j
@Service
public class CartServiceImpl implements CartService {
    @Resource
    private CartMapper CartMapper;

    @Override
    public AddItemRespDTO addItem(AddItemReqDTO request) {
        log.info("addItem rpc 服务被调用");
        CartItemVO item = request.getItem();
        Cart cart = Cart.builder()
                .userId(request.getUserId())
               .productId(item.getProductId())
               .quantity(item.getQuantity())
               .build();
        int num = CartMapper.insert(cart);
        if(num <= 0) {
            log.info("购物车插入失败");
            throw new BizException(100, "购物车插入失败");
        }
        return AddItemRespDTO.builder().build();
    }
    @Override
    public GetCartRespDTO getCart(GetCartReqDTO request) {
        log.info("getCart rpc 服务被调用");
        Long userId = request.getUserId();
        if(userId <= 0) {
            log.info("用户Id错误");
            throw new BizException(100, "用户Id错误");
        }

        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId);
        var cartList = CartMapper.selectList(queryWrapper);

        if(cartList == null) {
            log.warn("购物车为空");
            throw new BizException(100, "购物车为空");
        }

        CartVO cartResp = CartVO.builder().userId(userId).items(new ArrayList<>()).build();
        var items = cartResp.getItems();
        for(Cart cart : cartList) {
            items.add(CartItemVO.builder()
                   .productId(cart.getProductId())
                   .quantity(cart.getQuantity())
                   .build());
        }
        return GetCartRespDTO.builder()
                .cart(cartResp)
                .build();
    }

    @Override
    public EmptyCartRespDTO emptyCart(EmptyCartReqDTO request) {
        log.info("emptyCart rpc 服务被调用");

        Long userId = request.getUserId();

        if(userId <= 0) {
            log.warn("用户Id错误");
            throw new BizException(100, "用户Id错误");
        }
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId, userId);
        int nums = CartMapper.delete(queryWrapper);

        if(nums <= 0) {
            log.warn("购物车清空失败");
            throw new BizException(100, "购物车清空失败");
        }
        return EmptyCartRespDTO.builder().build();
    }
}
